<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./swiper/swiper.css">
  <style>
    .swiper-slide{
      height: 300px;
      background-color: rgb(208, 84, 84);
    }
    .swiper-slide img{
      width: 100%;
    }
    .swiper-slide:nth-child(2n){
      background-color: rgb(126, 187, 138);
    }
  </style>
</head>

<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./axios.js"></script>
  <script src="./swiper/swiper.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
    });
    const CommonHead = {
      template: `
        <div>
          <h2>这是组件1</h2>
          <div class="swiper">
            <div class="swiper-wrapper">
              <div 
                class="swiper-slide"
                v-for="banner in banners"
                :key="banner.id"
              >
                <img :src="banner.picUrl"/>
              </div>
            </div>
          </div>
        </div>
      `,
      
      data() {
        return {
          swiper: null,
          banners: []
        }
      },
      watch: {
        banners: {
          handler(){
            this.swiper.update();
          },
          deep: true,
          flush: 'post'
        }
      },
      methods: {
        fetchBanners() {
          axios.get('https://api.it120.cc/conner/banner/list').then(res => {
            if(res.data.code === 0) {
              this.banners = res.data.data
              /* this.$nextTick(() => {
                this.swiper.update();
              }) */
            }
          })
        },
        initSwiper(){
          this.swiper = new Swiper('.swiper', {
            autoplay: true,//可选选项，自动滑动
          })
        }
      },
      created() {
        this.fetchBanners()
      },
      mounted() {
        this.initSwiper()
      }
    }
    app.component('CommonHead', CommonHead)


    app.mount('#app')
  </script>
</body>

</html>